package Liti.LT_01;

import java.util.Arrays;
import java.util.Scanner;

public class Nd_01 {

    public static void main(String[] args) {
        int n, d;//定义两个数据
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        n *= n;
        d = in.nextInt();//输入n，d
        int[] b = new int[n];//存放数据的数组
        int sum = 0;//计算数组总和
        for (int i = 0; i < n; i++) {
            b[i] = in.nextInt();
            if (i > 0 && b[i] % d != b[i - 1] % d) {
                System.out.println("-1");
                System.exit(0);
            }
            sum += b[i];
        }
        Arrays.sort(b);
        int x = b[0] % d;
        if(x==0) x=1;
        int y = (sum - (x * n)) / d;
        System.out.println(y);

    }
}
